package rs.udd.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import rs.udd.exceptions.UddException;
import rs.udd.model.Advertiser;
import rs.udd.model.Agent;
import rs.udd.model.Link;
import rs.udd.repository.AdvertiserRepository;
import rs.udd.repository.LinkRepository;

@Service
@Transactional
public class LinkServiceImpl implements LinkService{
	
	@Resource
	private LinkRepository linkRepository;
	
	@Resource
	private AdvertiserRepository advertiserRepository;

	@Override
	public Link save(Link t) throws UddException {
		try {
			return linkRepository.save(t);
		} catch (Exception e) {
			throw new UddException("Udd exception", e);
		}
	}

	@Override
	public void delete(Long id) throws UddException {
		try {
			linkRepository.delete(id);
		} catch (Exception e) {
			throw new UddException("Udd exception", e);
		}
	}

	@Override
	public Link findOne(Long id) throws UddException {
		try {
			return linkRepository.findOne(id);
		} catch (Exception e) {
			throw new UddException("Udd exception", e);
		}
	}

	@Override
	public List<Link> findAll() throws UddException {
		try {
			return linkRepository.findAll();
		} catch (Exception e) {
			throw new UddException("Udd exception", e);
		}
	}

	@Override
	public String generateAndSaveAdvertiserLink(Advertiser advertiser, String newEmail) {
		
		Link link = linkRepository.findByAdvertiser(advertiser);
		
		if (link != null) {
			linkRepository.delete(link);
		}
		
		link = new Link();
		
		String randomString = RandomStringUtils.random(40, true, true);
		
		
		link.setAdvertiser(advertiser);
		link.setRandomString(randomString);
		link.setNewEmail(newEmail);
		
		linkRepository.save(link);
		
		return randomString;
	}

	@Override
	public String generateAndSaveAgentLink(Agent agent) {
		String randomString = RandomStringUtils.random(20);
		
		Link link = new Link();
		
		link.setAgent(agent);
		link.setRandomString(randomString);
		
		linkRepository.save(link);
		
		return randomString;
	}

	@Override
	public boolean activateEmail(String randomString) throws UddException {
		Link link = linkRepository.findByRandomString(randomString);
		
		if (link == null) {
			return false;
		} else if (link.getAdvertiser() != null) {
			Advertiser advertiser = link.getAdvertiser();
			
			// check if the difference between creation date of activation link and the of activation is more than 24 hours (in milliseconds)
			if (new Date().getTime() - link.getCreationTime().getTime() > 1000*60*60*24) {
				linkRepository.delete(link);
				return false;
			} else {
				advertiser.setEmailActive(true);
				advertiser.setActive(true);
				advertiser.setEmail(link.getNewEmail());
				advertiserRepository.save(advertiser);
				linkRepository.delete(link);
				
				return true;
			}
			
		} else {
			throw new UddException("Invalid data in link table in DB (Reference to the Advertiser doesn't exist)");
		}
	}

	@Override
	public boolean activateAdvertiser(String randomString) throws UddException {
		Link link = linkRepository.findByRandomString(randomString);
		
		if (link == null) {
			return false;
		} else {
			Advertiser advertiser = link.getAdvertiser();
			advertiser.setActive(true);
			advertiser.setEmailActive(true);
			
			advertiserRepository.save(advertiser);
			linkRepository.delete(link);
			
			return true;
		}
	}

}
